package br.unicamp.integralization.tests;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

import br.unicamp.integralization.dp.PotaSolver;
import br.unicamp.integralization.dto.Allocation;
import br.unicamp.integralization.dto.History;

import com.google.gson.Gson;

public class Dp {

	
	public static void main(String[] args) {
		String ra = "690967";
		//String ra = "691067";
		//String ra = "690667";
//		String ra = "031015";
		History hist = null;
		String requestURL = "http://www.students.ic.unicamp.br/~ra104864/mc857/servidorDac/rest/obter_dados.php"
				+ "?ra=" + ra;
		try {
			System.out.println(requestURL);
			InputStream input = new URL(requestURL).openStream();
			Reader reader = new InputStreamReader(input, "UTF-8");

			hist = (History) new Gson().fromJson(reader, History.class);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

//		try {
//			 Thread.sleep(20000);
			System.out.println(hist);
			
			System.out.println("Start");
			long nano = System.nanoTime();
			Allocation bestAllocation = PotaSolver.execute(hist);
			//System.out.println(hist);
			System.out.println("Tempo para processar :"
					+ (System.nanoTime() - nano)/1000000 + " ms");
			
			if(bestAllocation==null){
				System.out.println("Não se formou");
			}else{
				System.out.println("Se formou:");
				System.out.println(bestAllocation);
				
			}
			
			
			
			String params = "ra=" + ra + "&alocacao="
					+ new Gson().toJson(bestAllocation);
			System.out.println(params);
			/*RequestDTO result;
			try {
				result = Bab.downloadUrl(requestURL, "POST", params);
			} catch (Exception e) {
				result = new RequestDTO(
						RequestStatusEnum.CONNECTION_ERROR.code(), null, null);
			}

			if (result.getStatus() == RequestStatusEnum.OK.code()) {
				System.out.println("OK =D");
			} else {
				System.out.println("D:");
			}*/

		/*} catch (Exception e) {
			System.err.println("Isso jamais deveria ter acontecido D:");
			e.printStackTrace();
		}*/
	}
}
